package com.timmy.bledemo;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Timmy on 2017/4/4.
 */

public class MyAdapter extends BaseAdapter {

    private Context mContext;
    private List<DeviceInfo> mList;
    private LayoutInflater mInflater;

    public MyAdapter(Context context) {
        mContext = context;
        mList = new ArrayList<>();
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item, null);
        }

        DeviceInfo info = mList.get(position);
        BluetoothDevice device = info.getDevice();

        TextView tv = (TextView) convertView.findViewById(R.id.item_tv);

        tv.setText(device.getName() + "-" + device.getAddress());

        return convertView;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    public void setData(List<DeviceInfo> list) {
        mList.clear();
        mList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public BluetoothDevice getItem(int position) {
        return mList.get(position).getDevice();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
}
